import os
from flask import Flask
from flask_migrate import Migrate
from ext import db
from settings import envs
from app.user.views import user_bp
from app.good.views import good_bp
from app.order.views import order_bp

migrate = Migrate()


def create_app():
    # 创建一个Flask对象
    app = Flask(__name__)
    # 从环境配置文件获取当前环境
    env = os.environ.get("FLASK_ENV", "dev")
    # 获取相应的配置类
    app.config.from_object(envs.get(env))
    # db数据库初始化
    db.init_app(app)
    migrate.init_app(app, db)
    # 注册蓝图
    app.register_blueprint(user_bp, url_prefix="/user")
    app.register_blueprint(good_bp, url_prefix="/good")
    app.register_blueprint(order_bp, url_prefix="/order")
    return app
